package com.misic.wed;

import com.alibaba.fastjson.JSONObject;
import com.misic.pojo.Collect;
import com.misic.service.CollectService;
import com.misic.utils.Consts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/collect")
public class CollectController {
    @Autowired
    private CollectService collectService;
    /**
     * 添加收藏  评论功能
     *
     */
    @PostMapping(value = "/add")
    public Object addCollect(Collect collect){
        JSONObject jsonObject = new JSONObject();
//        String userId = request.getParameter("userId");           //用户id
//        String type = request.getParameter("type");               //收藏类型（0歌曲1歌单）
//        String songId = request.getParameter("songId");           //歌曲id
        if(collect.getUserId()==null || collect.getUserId().equals("")){
            jsonObject.put(Consts.CODE,0);
            jsonObject.put(Consts.MSG,"收藏歌曲为空");
            return jsonObject;
        }
        if(collectService.existSongId(collect.getUserId(),collect.getSongId())){
            jsonObject.put(Consts.CODE,2);
            jsonObject.put(Consts.MSG,"已收藏");
            return jsonObject;
        }

        //保存到收藏的对象中
//        Collect Collect = new Collect();
//        Collect.setUserId(Integer.parseInt(userId));
//        Collect.setType(new Byte(type));
//        Collect.setSongId(Integer.parseInt(songId));

        boolean flag = collectService.insert(collect);
        if(flag){   //保存成功
            jsonObject.put(Consts.CODE,1);
            jsonObject.put(Consts.MSG,"收藏成功");
            return jsonObject;
        }
        jsonObject.put(Consts.CODE,0);
        jsonObject.put(Consts.MSG,"收藏失败");
        return jsonObject;
    }

    /**
     * 删除收藏
     */
    @GetMapping(value = "/delete")
    public Object deleteCollect(@RequestParam("userId")Integer userId,@RequestParam("songId")Integer songId){
//        String userId = request.getParameter("userId");           //用户id
//        String songId = request.getParameter("songId");           //歌曲id
        boolean flag = collectService.deleteByUserIdSongId(userId,songId);
        return flag;
    }
    /**
     * 查询某个用户的收藏列表
     */
    @GetMapping(value = "/collectOfUserId")
    public Object collectOfUserId(@RequestParam("userId")Integer  userId){
//        String userId = request.getParameter("userId");          //用户id
        return collectService.collectOfUserId(userId);
    }
    /**
     * 查询所有收藏
     */
    @GetMapping(value = "/allCollect")
    public Object allCollect(){
        return collectService.allCollect();
    }




}
